import { Prompt } from '@kit.ArkUI';
import { FileManager } from '../util/file_manager/FileManager';
import { AppConfig } from '../app_config/AppConfig';
import { EmailAddress, EmailProperties } from '../BaseMailEntity/BaseMailEntity';
import { getTaskLogger } from '../logger/Logger';

export const DebugAccountsFile: string = "DebugAccounts.json";

export const DebugAppConfigFile: string = "DebugAppConfig.json"

export const DebugSendMailFile: string = "DebugSendMail.json"

//定义几个常用的logger
export const syncLogger = getTaskLogger("__sync")

export const loginLogger = getTaskLogger("__login")

export const detailTag = '__detail'

export const draftTag = '__draft'

export const composeLogger = getTaskLogger("__compose")

function getAppConfig(isDebug: boolean): AppConfig {
  if (isDebug && FileManager.isRawFileExist(DebugAppConfigFile)) {
    let fileName = DebugAppConfigFile;
    let jsonString: string = FileManager.readTextFromRawFileSync(fileName);
    let appConfig: AppConfig = JSON.parse(jsonString);
    return appConfig;
  }
  return appConfig
}

export function getDebugMail(): DebugMail | undefined {
  if (appConfig.isAutoFillSendMail && FileManager.isRawFileExist(DebugSendMailFile)) {
    let jsonString: string = FileManager.readTextFromRawFileSync(DebugSendMailFile);
    let debugMail: DebugMail = JSON.parse(jsonString);
    return debugMail;
  }
  return undefined
}

//正式环境配置,不要修改.
export let appConfig: AppConfig = {
  "showDebugLog": false,
  "isAutoFillAccount": false,
  "isAutoFillSendMail": false,
  "isDebug": false,
  "showDebugToast": false,
  "enableSyncEntire": true,
  "enableSyncTimer": true,
  "enableLoginSync": true,
  "isOnlySyncFolder": false,
  "showFolderMailCount": false,
  "showSyncFailResult": false,
  "enableSaveDraftTimer": true,
  "isUseInNetTestAccount": false,
  "ignoreFirstNotify": true
}

export interface DebugMail {
  tos?: EmailAddress[],
  bccs?: EmailAddress[],
  ccs?: EmailAddress[],
  subject?: string,
  content?: string
}

/**
 * 基础debug组件
 */
export class DebugManager {
  static emailProperties: EmailProperties[] | null = null;
  /**
   * 调试输出方法.
   * TODO: 使用该方法的的message可以用字面量,之后一起删除
   * @param message
   * @param message
   */
  static showToast(message: string) {
    if (appConfig.showDebugToast) {
      Prompt.showToast({ message: message });
    }
  }




  /**
   * false情况下是正式环境,所有控制变量全部设置一遍.防止初始值设置不正确.
   * @param open
   */
  // static initDebug(isDebug: boolean) {
  //   appConfig = getAppConfig(isDebug);
  //   showDebugLog(appConfig.showDebugLog)
  // }
}